package com.it.smartoldb.controller;

import com.it.smartoldb.common.Result;
import com.it.smartoldb.entity.Staff;
import com.it.smartoldb.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 护理人员管理控制器
 */
@RestController
@RequestMapping("/api/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 创建护理人员
     */
    @PostMapping
    public Result<Staff> createStaff(@RequestBody Staff staff) {
        try {
            Staff created = staffService.createStaff(staff);
            return Result.success(created);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 更新护理人员信息
     */
    @PutMapping("/{id}")
    public Result<Staff> updateStaff(@PathVariable Long id, @RequestBody Staff staff) {
        try {
            Staff updated = staffService.updateStaff(id, staff);
            return Result.success(updated);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 删除护理人员
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteStaff(@PathVariable Long id) {
        try {
            staffService.deleteStaff(id);
            return Result.success();
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 根据ID查找护理人员
     */
    @GetMapping("/{id}")
    public Result<Staff> getStaffById(@PathVariable Long id) {
        Optional<Staff> staff = staffService.findById(id);
        if (staff.isPresent()) {
            return Result.success(staff.get());
        } else {
            return Result.error(404, "护理人员信息不存在");
        }
    }

    /**
     * 分页查询护理人员
     */
    @GetMapping
    public Result<Page<Staff>> getStaffList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String idCard,
            @RequestParam(required = false) String position,
            @RequestParam(required = false) String status) {
        
        Pageable pageable = PageRequest.of(page, size);
        
        Staff.StaffStatus statusEnum = null;
        if (status != null && !status.isEmpty()) {
            try {
                statusEnum = Staff.StaffStatus.valueOf(status);
            } catch (IllegalArgumentException e) {
                return Result.error(400, "无效的护理人员状态");
            }
        }
        
        Page<Staff> staffPage = staffService.findStaffWithFilters(name, idCard, position, statusEnum, pageable);
        return Result.success(staffPage);
    }

    /**
     * 根据职位查找护理人员
     */
    @GetMapping("/position/{position}")
    public Result<List<Staff>> getStaffByPosition(@PathVariable String position) {
        List<Staff> staffList = staffService.findByPosition(position);
        return Result.success(staffList);
    }

    /**
     * 根据状态查找护理人员
     */
    @GetMapping("/status/{status}")
    public Result<List<Staff>> getStaffByStatus(@PathVariable String status) {
        try {
            Staff.StaffStatus staffStatus = Staff.StaffStatus.valueOf(status);
            List<Staff> staffList = staffService.findByStatus(staffStatus);
            return Result.success(staffList);
        } catch (IllegalArgumentException e) {
            return Result.error(400, "无效的护理人员状态");
        }
    }

    /**
     * 根据姓名模糊查询护理人员
     */
    @GetMapping("/search")
    public Result<List<Staff>> searchStaffByName(@RequestParam String name) {
        List<Staff> staffList = staffService.findByNameContaining(name);
        return Result.success(staffList);
    }

    /**
     * 获取所有在职人员
     */
    @GetMapping("/active")
    public Result<List<Staff>> getActiveStaff() {
        List<Staff> staffList = staffService.findActiveStaff();
        return Result.success(staffList);
    }

    /**
     * 根据工作经验查找人员
     */
    @GetMapping("/experience/{minExperience}")
    public Result<List<Staff>> getStaffByExperience(@PathVariable Integer minExperience) {
        List<Staff> staffList = staffService.findByMinExperience(minExperience);
        return Result.success(staffList);
    }

    /**
     * 根据技能查找人员
     */
    @GetMapping("/skill/{skill}")
    public Result<List<Staff>> getStaffBySkill(@PathVariable String skill) {
        List<Staff> staffList = staffService.findBySkill(skill);
        return Result.success(staffList);
    }

    /**
     * 获取护理人员统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStaffStatistics() {
        Map<String, Object> statistics = Map.of(
                "totalCount", staffService.getTotalCount(),
                "activeCount", staffService.getActiveCount(),
                "positionStats", staffService.getPositionStatistics()
        );
        return Result.success(statistics);
    }

    /**
     * 停职/离职护理人员
     */
    @PutMapping("/{id}/deactivate")
    public Result<Staff> deactivateStaff(@PathVariable Long id) {
        try {
            Staff staff = staffService.deactivateStaff(id);
            return Result.success(staff);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 恢复在职状态
     */
    @PutMapping("/{id}/activate")
    public Result<Staff> activateStaff(@PathVariable Long id) {
        try {
            Staff staff = staffService.activateStaff(id);
            return Result.success(staff);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 根据身份证号查找护理人员
     */
    @GetMapping("/id-card/{idCard}")
    public Result<Staff> getStaffByIdCard(@PathVariable String idCard) {
        Optional<Staff> staff = staffService.findByIdCard(idCard);
        if (staff.isPresent()) {
            return Result.success(staff.get());
        } else {
            return Result.error(404, "未找到对应身份证号的护理人员信息");
        }
    }
} 